package com.example.servlet.auth;

import com.example.entity.Manager;
import com.example.entity.User;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

@WebServlet("/user_logout")
public class LogoutServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User user = (User) req.getSession().getAttribute("user");
        if (user != null) {
            req.getSession().removeAttribute("user");
            Cookie cookie_username = new Cookie("u_account", "");
            cookie_username.setMaxAge(0);
            Cookie cookie_password = new Cookie("u_password", "");
            cookie_password.setMaxAge(0);
            resp.addCookie(cookie_username);
            resp.addCookie(cookie_password);
            resp.sendRedirect("host");
        } else {
            req.getSession().removeAttribute("manager");
            Cookie cookie_m_username = new Cookie("m_account", "");
            cookie_m_username.setMaxAge(0);
            Cookie cookie_m_password = new Cookie("m_password", "");
            cookie_m_password.setMaxAge(0);
            resp.addCookie(cookie_m_username);
            resp.addCookie(cookie_m_password);
            resp.sendRedirect("host");
        }
    }
}
